import logging
from .mobility import (
    Position,
    ConstantSpeed,
    MobilityModel,
    HoverMobility
)
from .record import recorder

logger = logging.getLogger('Node')

class Node:

    def __init__(self, env, id):
        self.env = env
        self.id = id
        self.last_ts = 0
        # 默认位置在0点
        self.position = Position(0, 0, 0)
        # 默认无移动性
        self.mobility = HoverMobility(ConstantSpeed(0))

    def set_phy(self, phy):
        self.phy = phy

    def set_mac(self, mac):
        self.mac = mac
    
    def move(self):
        dt = 0.5
        while True:
            self.mobility.step(self, dt)
            logger.debug(f'{self.env.now:.6f} {self.id} {self.position}')
            recorder.record_postion(self)
            yield self.env.timeout(dt)
    
    def set_position(self, position:Position):
        self.position = position
    
    def set_mobility(self, mobility:MobilityModel):
        """设置节点移动性"""
        self.mobility = mobility
        self.env.process(self.move())
    